/**
 * @file:   yuvConvert.c
 * @note:   2010-2020, <git.oschia.net/think3r>
 * @brief:  YUV <-> RGB 转换实现
 * @author: think3r
 * @date    2018/7/2
 * @note:
 * @note \n History:
   1.日    期: 2018/7/2
     作    者: think3r
     修改历史: 创建文件
 */

/*----------------------------------------------*/
/*                  头文件包含                  */
/*----------------------------------------------*/
#include <string.h>
#include "common.h"
#include "yuvConvert.h"

/*----------------------------------------------*/
/*                  宏类型定义                  */
/*----------------------------------------------*/

//#define __DEBUG__  

#ifdef __DEBUG__  
    #define DEBUG(format,...) printf("*["__FILE__" L:%04d] |  "format"", __LINE__, ##__VA_ARGS__)  
#else  
    #define DEBUG(format,...)  
#endif  

/*----------------------------------------------*/
/*                 结构体定义                   */
/*----------------------------------------------*/

/*----------------------------------------------*/
/*                  函数声明                    */
/*----------------------------------------------*/

/*----------------------------------------------*/
/*                  全局变量                    */
/*----------------------------------------------*/

/*----------------------------------------------*/
/*                  函数定义                    */
/*----------------------------------------------*/

/**
 * @function:   GetFileSize
 * @brief:      获取  文件大小
 * @param[in]:  char * file_path   
 * @param[out]: UINT * file_size  
 * @param[out]: None
 * @return:     ERR
 */
ERR GetFileSize(char * file_path, long * file_size)  
{     
    FILE *infile = NULL;  
    ERR err = OK;
    
    if((infile=fopen(file_path,"rb"))==NULL)  
    {  
        printf( "\nERROR!! Can not open the path: [%s] \n", file_path); 
        
        err = FILE_NOT_EXIT;
        return err;
    }  

    if(NULL == file_size)
    {
       err = PARAM_INVALID;
       printf("[%s %d]  ERROR!! PARAM_INVALID\n",__FUNCTION__,__LINE__);
       
       return err;
    }
    
    *file_size = 0;
    fseek(infile, 0, SEEK_END);      /*移动到文件末尾*/  
    *file_size = ftell(infile);      /*定位文件位置指针的位置*/
    fclose(infile);    
    
    DEBUG("\tfile_Siez:[%u]\n", *file_size);        

    return OK;  
} 

/**
 * @function:   read_file
 * @brief:      读取文件
 * @param[in]:  char *path     
 * @param[in]:  UCHAR *outbuf  
 * @param[in]:  UINT size      
 * @param[out]: None
 * @return:     ERR
 */
ERR read_file(char *path, UCHAR *outbuf, UINT size)  
{  
    FILE *infile = NULL;  
    ERR err = OK;
    
    if((infile=fopen(path,"rb"))==NULL)  
    {  
        printf( "\nCan not open the path: %s \n", path); 
        
        err = FILE_NOT_EXIT;
        return err;
    }  

    if(size > 256)
    {
        size = 256;
        printf("[%s %d] err size:[%d]\n",__FUNCTION__, __LINE__, size);
    }
    fread(outbuf, sizeof(UCHAR), size, infile);    
    fclose(infile);  

    return err;
}

/**
 * @function:   decodeInputFromate
 * @brief:      从字符串解析 YUV 格式
 * @param[in]:  char * strFromate         
 * @param[in]:  YUV_FORMATE * yuvFromate  
 * @param[out]: None
 * @return:     ERR
 */
ERR decodeInputFromate(char * strFromate, YUV_FORMATE * yuvFromate)
{
    ERR err = ok;

    if( strFromate == NULL || yuvFromate == NULL)
    {
       err = PARAM_INVALID;
       printf("[%s %d]  ERROR!! PARAM_INVALID\n",__FUNCTION__,__LINE__);
       
       return err;
    }

    if(strstr(strFromate, "I420") || strstr(strFromate, "YU12"))
    {
        *yuvFromate = YUV_420_I420;
        return ok;
    }
    else if(strstr(strFromate, "YV12"))
    {
        *yuvFromate = YUV_420_YV12;
        return ok;
    }
    else if(strstr(strFromate, "NV12"))
    {
        *yuvFromate = YUV_420_NV12;
        return ok;
    }
    else if(strstr(strFromate, "NV21"))
    {
        *yuvFromate = YUV_420_NV21;
        return ok;
    }
    else if(strstr(strFromate, "422P"))
    {
        *yuvFromate = YUV_422_422P;
        return ok;
    }
    else if(strstr(strFromate, "422SP"))
    {
        *yuvFromate = YUV_422_422SP;
        return ok;
    }
    else if(strstr(strFromate, "UYVY"))
    {
        *yuvFromate = YUV_422_UYVY;
        return ok;
    }
    else if(strstr(strFromate, "YUYV"))
    {
        *yuvFromate = YUV_422_YUYV;
        return ok;
    }

    return PARAM_INVALID;
}

